<template>
  <van-icon
    :class="{
      liked: value === 1
    }"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="dianzan"
    :loading="loading"
  />
</template>

<script>
import { goodarticle, delarticle } from '@/api/article'
export default {
  name: 'likeArticleIndex',
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async dianzan () {
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          // 已点赞，取消点赞
          await delarticle(this.userId)
          this.$toast.fail('取消点赞')
        } else {
          // 未点赞，添加点赞
          await goodarticle(this.userId)
          this.$toast.success('点赞成功')
          status = 1
        }
        this.$emit('input', status)
      } catch (err) {
        this.$toast('操作失败，请重试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
  .liked {
  color:red
}
</style>
